﻿using System;
using System.Collections.Generic;
using System.Text;

//world list
using WorldList = System.Collections.Generic.List<int>;
//group map
using WorldGroupInfoMap = System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<int>>;

/**
*@file       worldgroupreader.cs
*@brief      load group config
*
*@author     graywind
*@version    1.0.0.1
*@data       2012/03/01
*/

public class WorldGroupReader
{
    #region public constant
    //invalid group number
    public const int INVALID_GROUP = -1;
    #endregion
    #region private variable
    //group information
    private WorldGroupInfoMap m_infoMap = new WorldGroupInfoMap();
    #endregion

    #region external call
    /// <summary>
    /// load config file
    /// </summary>
    /// <param name="file_name"></param>
    public void LoadFromFile(string file_name)
    {
        try
        {
            using (System.IO.FileStream file = new System.IO.FileStream(file_name, System.IO.FileMode.Open))
            {
                global.WorldGroupInfo wgi = ProtoBuf.Serializer.Deserialize<global.WorldGroupInfo>(file);
                if (wgi.Type != global.WorldGroupInfo.ServerType.GameServer)
                {
                    System.Console.WriteLine("Can Not Read World Group Config File:{0} File Type Not Right!", file_name);
                    return;
                }

                foreach (global.WorldGroupInfo.WorldGroup wg in wgi.Info)
                {
                    WorldList wl = new WorldList();
                    foreach (int id in wg.WorldId)
                    {
                        wl.Add(id);
                    }
                    m_infoMap[wg.WorldGroupId] = wl;
                }
            }
        }
        catch (System.Exception e)
        {
            System.Console.WriteLine("Can Not Read World Group Config File:{0} With Error:{1}", file_name, e.Message);
        }
    }

    /// <summary>
    /// get group information
    /// </summary>
    /// <param name="group_id"></param>
    /// <returns></returns>
    public WorldList GetGroupInfo(int group_id)
    {
        WorldList wl;
        if (m_infoMap.TryGetValue(group_id, out wl))
            return wl;

        return null;
    }
    #endregion
}